package com.ywjt.platformbusiness.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.my.adapter.BalanceAdapter;
import com.ywjt.platformbusiness.my.model.BalanceModel;
import com.ywjt.platformbusiness.my.model.IntegralModel;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyBalanceActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    private RecyclerView rcCommon;
    private BalanceAdapter balanceAdapter;
    private SmartRefreshLayout refresh;
    private TextView tvBalance,tvConsumeBalance;
    private TextView tvWithDraw,tvInvest;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyBalanceActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvWithDraw:
                ActivityWithdraw.actionStart(this);
                break;
            case R.id.tvInvest:
                ActivityInvest.actionStart(this);
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(false);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_mybalance;
    }

    @Override
    protected void initView() {
        ivBack=findViewById(R.id.ivBack);
        rcCommon=findViewById(R.id.rcCommon);
        tvWithDraw=findViewById(R.id.tvWithDraw);
        tvConsumeBalance=findViewById(R.id.tvConsumeBalance);
        tvBalance=findViewById(R.id.tvBalance);
        tvInvest=findViewById(R.id.tvInvest);
        refresh=findViewById(R.id.refresh);
        tvWithDraw.setOnClickListener(this::onClick);
        ivBack.setOnClickListener(this::onClick);
        tvInvest.setOnClickListener(this::onClick);

        getData();
    }

    @Override
    protected void initData() {
    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    public void getData(){
        new HttpRequest(getContext()).doGet(UrlConstants.getUserBalanceMessage, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), BalanceModel.class);
                        if (obj instanceof BalanceModel) {
                            BalanceModel balanceModel =(BalanceModel)obj;

                            tvBalance.setText(balanceModel.getResult().getBalance()+"");
                            tvConsumeBalance.setText(balanceModel.getResult().getConsumeBalance()+"");

                            if (balanceModel.getResult().getBalanceRecordList()!=null&&
                                    balanceModel.getResult().getBalanceRecordList().size()> 0) {
                                /*****/
                                balanceAdapter=new BalanceAdapter(balanceModel.getResult().getBalanceRecordList());
                                rcCommon.setLayoutManager(new LinearLayoutManager(getContext()));
                                rcCommon.setAdapter(balanceAdapter);
                            } else {
                                refresh.finishRefreshWithNoMoreData();
                            }

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }
}
